package timing.ukulele.portal.controller;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RestController;
import timing.ukulele.common.data.ResponseData;
import timing.ukulele.data.portal.view.SysMenuPermissionVO;
import timing.ukulele.facade.portal.mvc.SysMenuPermissionControllerFacade;
import timing.ukulele.portal.convertor.PermissionConverter;
import timing.ukulele.portal.persistent.SysMenuPermission;
import timing.ukulele.portal.service.SysMenuPermissionService;

import java.util.Date;
import java.util.List;

@RestController
public class SysMenuPermissionController implements SysMenuPermissionControllerFacade {
    private final SysMenuPermissionService service;

    public SysMenuPermissionController(SysMenuPermissionService service) {
        this.service = service;
    }

    @Override
    public ResponseData<SysMenuPermissionVO> permission(Long id) {
        SysMenuPermission po = this.service.getById(id);
        if (po != null) {
            SysMenuPermissionVO vo = PermissionConverter.INSTANCE.p2v(po);
            return ResponseData.success(vo);
        }
        return ResponseData.success();
    }

    @Override
    public ResponseData<Boolean> add(String currentUser, SysMenuPermissionVO vo) {
        if (vo == null || vo.getMenuId() == null || vo.getId() != null) {
            return ResponseData.paramError();
        }
        SysMenuPermission po = PermissionConverter.INSTANCE.v2p(vo);
        this.service.save(po);
        return ResponseData.success();
    }

    @Override
    public ResponseData<Boolean> edit(String currentUser, SysMenuPermissionVO vo) {
        if (vo == null || vo.getMenuId() == null || vo.getId() == null) {
            return ResponseData.paramError();
        }
        SysMenuPermission po = PermissionConverter.INSTANCE.v2p(vo);
        this.service.updateById(po);
        return ResponseData.success();
    }

    @Override
    public ResponseData<Boolean> delete(String currentUser, Long id) {
        this.service.lambdaUpdate()
                .set(SysMenuPermission::getDeleted, Boolean.TRUE)
                .set(SysMenuPermission::getUpdateBy, currentUser)
                .set(SysMenuPermission::getUpdateTime, new Date())
                .eq(SysMenuPermission::getId, id).update();
        return ResponseData.success();
    }

    @Override
    public ResponseData<List<SysMenuPermissionVO>> findByMenu(Long menuId, Boolean deleted) {
        LambdaQueryChainWrapper<SysMenuPermission> query = this.service.lambdaQuery().eq(SysMenuPermission::getMenuId, menuId);
        if (deleted != null)
            query.eq(SysMenuPermission::getDeleted, deleted);
        List<SysMenuPermission> list = query.list();
        if (!CollectionUtils.isEmpty(list)) {
            List<SysMenuPermissionVO> voList = PermissionConverter.INSTANCE.p2vList(list);
            return ResponseData.success(voList);
        }
        return ResponseData.success();
    }
}
